package ltd.dujiabao.dex.rabbitmq.exe;

import java.lang.reflect.Method;

import org.springframework.amqp.rabbit.core.RabbitTemplate;

import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.dex.core.DelayJobEntity;
import ltd.dujiabao.dex.core.emitter.Emitter;
import ltd.dujiabao.dex.json.JsonUtils;
import ltd.dujiabao.dex.rabbitmq.domain.DelayMessage;
import ltd.dujiabao.dex.rabbitmq.domain.RabbitDefinition;

/**
 * @author : DuJiabao
 * @Project : common-delay-executor
 * @Time : 2024/3/27 22:41
 * @Desc :
 */
@Slf4j
public class RabbitEmitter implements Emitter {
    private final DelayJobEntity delayJob;
    private final RabbitTemplate rabbitTemplate;

    public RabbitEmitter(DelayJobEntity delayJob, RabbitTemplate rabbitTemplate) {
        this.delayJob = delayJob;
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        RabbitDefinition rabbitDefinition = RabbitDefinition.create(delayJob.identity());
        DelayMessage delayMessage = new DelayMessage()
                .setIdentity(delayJob.identity())
                .setClazz(method.getDeclaringClass())
                .setMethod(method.getName())
                .setParameterTypes(method.getParameterTypes())
                .setArguments(args);
        rabbitTemplate.convertAndSend(rabbitDefinition.getEmitQueueName(), JsonUtils.bean2Json(delayMessage));
        return null;
    }
}
